package com.mxprojects.constructors
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	
	public class ImageDisplay extends Sprite
	{		
		public static const ERROR_IMAGE_NOT_FOUND:String = "errorImageNotFound";
		
		public var startWidth:int;
		public var startHeight:int;
		
		private var imgLoader:Loader = new Loader();
		private var description:SimpleTextField;
		private var imageHolder:Sprite = new Sprite();
		private var processedImage:Bitmap;
		private var startScale:Number = 1;
		
		public function ImageDisplay(location:String, descriptionText:String, imgWidth:int, imgHeight:int, processAsBitmap:Boolean = true){			
			//make sure we have something to load
			if(location == null){
				return;
			}

			//set the vars
			startWidth = imgWidth;
			startHeight = imgHeight;
			
			//load the image
			imgLoader.load(new URLRequest(location));
			
			//listen for when the image is done
			if(processAsBitmap){
				imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, addImage, false, 0, true);
			}else{
				imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, addNonBitmapImage, false, 0, true);
			}
			imgLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, loadError, false, 0, true);
			
			//put the description below the image
			description = new SimpleTextField("<span class=\"postImageDescription\">" + descriptionText + "</span>", 1, true);
			description.y = imgHeight;
			addChild(description);
			
			//add the image holder
			addChild(imageHolder);
			
			//listen for the mouse if they want to view a full size
			//imageHolder.addEventListener(MouseEvent.ROLL_OVER, checkImageScale, false, 0, true);
		}
		
		public function getHeight():int {
			if(description != null){
				return Math.round(description.y + description.height);
			}else{
				return imageHolder.height;
			}
		}
		
		public function setWidth(newWidth:int):void {
			//this will scale the processed image to fit within the window.
			if(processedImage != null){
				if(newWidth >= startWidth){
					//reset the image
					processedImage.width = startWidth;
					processedImage.height = startHeight;
				}else{
					//use fancy math to scale it properly
					processedImage.scaleX = newWidth / startWidth;
					processedImage.scaleY = newWidth / startWidth;
				}
				
				//set the description
				description.y = processedImage.height;
			}else{
				//if not setup yet then set a start scale
				startScale = newWidth / startWidth;
				if(description != null){
					description.y = startHeight * startScale;
				}
			}
		}
		
		private function addImage(event:Event):void {
			//make a bitmap out of the loaded image so that we can do stuff to it if need be
			var bitmapData:BitmapData = new BitmapData(imgLoader.width, imgLoader.height);
			bitmapData.draw(imgLoader);
			
			//set a var that we can access elseware
			processedImage = new Bitmap(bitmapData, "auto", true);
			
			//scale it to what it should start at
			processedImage.scaleX = startScale;
			processedImage.scaleY = startScale;
			
			//set the description
			description.y = processedImage.height;
			
			//add it to the holder
			imageHolder.addChild(processedImage);
			
			//get rid of the imgLoader
			imgLoader.unload();
		}
		
		private function addNonBitmapImage(event:Event):void {
			//this provides a way to load images directly without bitmap processing
			imageHolder.addChild(imgLoader);
			
			//set the description
			description.y = imgLoader.height;
		}
		
		private function checkImageScale(event:MouseEvent):void {
			trace("The image scale is", processedImage.scaleX);
		}
		
		private function loadError(event:IOErrorEvent):void {
			dispatchEvent(new Event(ERROR_IMAGE_NOT_FOUND));
			trace("image didn't load", event.text);
		}

	}
}